//
//  PartsViewController.m
//  AdvertisementI
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 Apple inc. All rights reserved.
//

#import "PartsViewController.h"
#import "Masonry.h"
#import "netWork.h"
#import "MAin.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "ZLSoloViewController.h"

@interface PartsViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *mainName;

@end

@implementation PartsViewController
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //    设置导航栏的字体以及颜色
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //    添加滚动视图
    [self.view addSubview:self.scrollView];
    _friendArray = [[NSMutableArray alloc]init];
    //    请求数据
    [netWork scroll:^(NSArray *arr) {
        for (int i = 0; i<arr.count; i++) {
            MAin *main =[arr objectAtIndex:i ];
            [self.mainName addObject:main];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(15+i%2*(self.view.frame.size.width/2), -64+15+i/2*(self.view.frame.size.height-64-49-30)/3, (self.view.frame.size.width-60)/2, (self.view.frame.size.height-64-49-20)/3*2/3);
            [button sd_setImageWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://47.88.76.185:8080/Test/%@",main.image]] forState:UIControlStateNormal ];
            [self.scrollView addSubview:button];
            [button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i+1;
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15+i%2*(self.view.frame.size.width/2), button.frame.size.height+10-64+i/2*(self.view.frame.size.height-64-49-20)/3, (self.view.frame.size.width-60)/2, (self.view.frame.size.height-64-49-20)/3/3)];
            label.text = main.name;
            label.textAlignment =NSTextAlignmentCenter;
            [self.scrollView addSubview:label];
            label.textColor = [UIColor blueColor];
            label.font = [UIFont systemFontOfSize:18];
        }
    } type:@"app_loadSkill"];
}

#pragma mark --设置btn的背景随机色---
-(void)btnBackgroundcolor
{
    a=arc4random()%255/255.0;
    b=arc4random()%255/255.0;
    c=arc4random()%255/255.0;
}




#pragma mark   ---按钮回调事件---
-(void)butClick:(UIButton *)sender
{
    ZLSoloViewController *vc= [[ZLSoloViewController alloc]init];
   
    vc.hidesBottomBarWhenPushed = YES;
    NSInteger count = 0;
    count = sender.tag;
     MAin *main =[self.mainName objectAtIndex:count-1];
    //    NSString * string = nil;
    if (count ==1) {
        vc.myTitle = main.name;
        vc.string = @"http://47.88.76.185/mefragment/3D_NAND_Flash.html";
    }else if (count==2){
        vc.myTitle = main.name;
        vc.string = @"http://47.88.76.185/mefragment/3D_xpoint.html" ;
    }else if (count==3){
        vc.myTitle = main.name;
        vc.string =@"http://47.88.76.185/mefragment/Finfet.html" ;
    }else if (count==4){
        vc.myTitle = main.name;
        vc.string = @"http://47.88.76.185/mefragment/MEMS.html";
    }else if (count==5){
        vc.myTitle = main.name;
        vc.string = @"http://47.88.76.185/mefragment/DRAMS.html";
    }
    [self.navigationController pushViewController:vc animated:YES];
}





-(void)viewWillAppear:(BOOL)animated
{
    [self btnBackgroundcolor];
}








- (void) hitBUtton:(UIButton *)sender{
    
   
}





- (UIScrollView *)scrollView{
    
    if (!_scrollView) {

        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-49)];
        _scrollView.backgroundColor = [UIColor whiteColor];

    }
    return _scrollView;
}

- (NSMutableArray *)mainName {
    
    if (!_mainName) {
        
        _mainName = [[NSMutableArray alloc] init];
    }
    return _mainName;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
